﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace University.Workshop.Model.CardGame
{
    [Serializable]
    public class Deck
    {       
        private Collection<Card> ArrayCard;
        
        //Current card index
        private int ptrCurrentCard = 0;
        
        // shuffle variable
        private Random randomPointer = new Random(DateTime.Now.Millisecond);

        public Deck()
        {
            CreateListCards();
        }

        private void CreateListCards()
        {
            ptrCurrentCard = 0;
            ArrayCard = new Collection<Card>();
            //int Counter = 0;
           
            //Builting functionality of Enum
            foreach (EnumSuit Suit in Enum.GetValues(typeof(EnumSuit)))
                foreach (EnumRank Rank in Enum.GetValues(typeof(EnumRank)))
                    if (Rank != EnumRank.None && Suit != EnumSuit.None)
                    {
                        ArrayCard.Add(new Card(Rank, Suit));
                    }
        }

        public Card PullCard()
        {
            return ArrayCard[ptrCurrentCard++];
        }

        public Card PeekCard()
        {
            return ArrayCard[ptrCurrentCard];
        }

        private void swapCards(int i, int j)
        {
            Card temporalCard = ArrayCard[i];
            ArrayCard[i] = ArrayCard[j];
            ArrayCard[j] = temporalCard;
        }
        
        //Shuffle the deck and reset the current card index to the beginning
        public void Shuffle(int count)
        {
            ptrCurrentCard = 0;
            for (int i = 0; i < count; ++i)
            {
                for (int j = 0; j < 52; ++j)
                {
                    int idx = randomPointer.Next(52);
                    swapCards(j, idx);
                }
            }
        }

        
        //10 is overkill, 8 should be enough
        public void Shuffle()
        {
            this.Shuffle(10);
        }
    }
}
